﻿using NavigationDemo.Common;
using NavigationDemo.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace NavigationDemo
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
            Loaded += MainPage_Loaded;
            App.Current.Suspending += Current_Suspending;
        }

        void Dispatcher_AcceleratorKeyActivated(Windows.UI.Core.CoreDispatcher sender, Windows.UI.Core.AcceleratorKeyEventArgs args)
        {
            if (args.Handled)
                return;
            if (args.EventType == CoreAcceleratorKeyEventType.KeyDown || args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown)
                return;

            var _DataContext = (MyFrame.Content as Page).DataContext as SharedViewModel;
            if (_DataContext == null)
                return;

            if (args.VirtualKey == VirtualKey.Back || args.VirtualKey == VirtualKey.Left)
            // back
            {
                _DataContext.GoBackCommand.Execute(null);
                args.Handled = true;
            }
            else if (args.VirtualKey == VirtualKey.Right)
            // forward
            {
                _DataContext.GoForwardCommand.Execute(null);
                args.Handled = true;
            }
        }

        async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            SuspensionManager.RegisterFrame(MyFrame, "AppFrame");
            try { await SuspensionManager.RestoreAsync(); }
            catch (SuspensionManagerException) { /* error */ }
            MyFrame.Navigate(typeof(Views.GrandParent));
        }

        async void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
        {
            var _Deferral = e.SuspendingOperation.GetDeferral();
            await SuspensionManager.SaveAsync();
            _Deferral.Complete();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e) { MyFrame.Navigate(typeof(Views.GrandParent)); }
        private void Button_Click_2(object sender, RoutedEventArgs e) { MyFrame.Navigate(typeof(Views.Parent)); }
        private void Button_Click_3(object sender, RoutedEventArgs e) { MyFrame.Navigate(typeof(Views.Child)); }
        private void Button_Click_4(object sender, RoutedEventArgs e) { MyFrame.Navigate(typeof(Views.Grandchild)); }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            MyFrame.SetNavigationState(new Frame().GetNavigationState());
            MyFrame.Navigate(typeof(Views.GrandParent));
        }
    }
}
































